package com.example.cloud.controller.file;

import com.example.cloud.service.FileService;
import com.example.cloud.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/files")
@Tag(name = "文件接口", description = "文件上传相关接口")
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    
    private final FileService fileService;
    
    @Autowired
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    /**
     * 上传用户头像
     * @param file 头像文件
     * @param oldAvatarUrl 旧头像URL（可选）
     * @return 上传结果
     */
    @PostMapping("/avatar")
    @Operation(summary = "上传头像", description = "上传用户头像图片")
    public Result<Map<String, String>> uploadAvatar(
            @RequestParam("file") @Parameter(description = "头像文件") MultipartFile file,
            @RequestParam(value = "oldAvatarUrl", required = false) @Parameter(description = "旧头像URL") String oldAvatarUrl) {
        if (file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }

        try {
            // 调用服务层处理上传逻辑
            Map<String, String> data = fileService.uploadAvatar(file, oldAvatarUrl);
            return Result.success(data);
        } catch (Exception e) {
            logger.error("文件上传失败", e);
            return Result.error("文件上传失败: " + e.getMessage());
        }
    }
} 